home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / rayshade / graphtal.lzh / Graphtal.Amiga / ExprItems.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  13KB  |  524 lines

  1. /*
  2.  * ExprItems.C - methods for all the ExprItem's.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  *                     University of Berne, Switzerland
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified, and redistributed
  9.  * provided that this copyright notice is preserved on all copies.
  10.  *
  11.  * You may not distribute this software, in whole or in part, as part of
  12.  * any commercial product without the express consent of the authors.
  13.  *
  14.  * There is no warranty or other guarantee of fitness of this software
  15.  * for any purpose.  It is provided solely "as is".
  16.  *
  17.  */
  18.  
  19. #include <iostream.h>
  20. #include "ExprItems.h"
  21.  
  22. //___________________________________________________________ Variable
  23.  
  24. Variable::Variable(const Name& n, Value* val)
  25. : varname(n), theValue(val)
  26. {}
  27. void Variable::process(ValueStack* theStack)
  28. {
  29.   theStack->push(*theValue); 
  30. }
  31. Variable::~Variable(){}
  32. void Variable::print(ValueStack* theStack)
  33. {
  34.   theStack->push(Value((const char*)varname));
  35. }
  36. // there's no way to simplify a variable
  37. int Variable::params() { return -1; } 
  38. ExprItem* Variable::copy() { return new Variable(varname, theValue); }
  39.  
  40. //___________________________________________________________ Uminus
  41.  
  42. Uminus::Uminus(){}
  43. Uminus::~Uminus(){}
  44. void Uminus::process(ValueStack* theStack)
  45.   theStack->push(- theStack->pop()); 
  46. }
  47. void Uminus::print(ValueStack* theStack)
  48.   theStack->push(Value("-(" + (rcString)theStack->pop() + ")")); 
  49. }
  50. int Uminus::params() { return 1; }
  51. ExprItem* Uminus::copy() { return new Uminus; }
  52.  
  53. //___________________________________________________________ Not
  54.  
  55. Not::Not(){}
  56. Not::~Not(){}
  57. void Not::process(ValueStack* theStack)
  58. {
  59.   theStack->push(!theStack->pop());
  60. }
  61. void Not::print(ValueStack* theStack)
  62.   theStack->push(Value("!(" + (rcString)theStack->pop() + ")")); 
  63. }
  64. int Not::params() { return 1; }
  65. ExprItem* Not::copy() { return new Not; }
  66.  
  67. //___________________________________________________________ Or
  68.  
  69. Or::Or(){}
  70. Or::~Or(){}
  71. void Or::process(ValueStack* theStack)
  72. {
  73.   theStack->push(theStack->pop() || theStack->pop());
  74. }
  75. void Or::print(ValueStack* theStack)
  76. {
  77.   theStack->push(Value("(" + (rcString)theStack->pop() + " || " +
  78.                      (rcString)theStack->pop() + ")"));
  79. }
  80. int Or::params() { return 2; }
  81. ExprItem* Or::copy() { return new Or; }
  82.  
  83. //___________________________________________________________ And
  84.  
  85. And::And(){}
  86. And::~And(){}
  87. void And::process(ValueStack* theStack)
  88. {
  89.   theStack->push(theStack->pop() && theStack->pop());
  90. }
  91. void And::print(ValueStack* theStack)
  92. {
  93.   theStack->push(Value("(" + (rcString)theStack->pop() + " && " +
  94.                      (rcString)theStack->pop() + ")"));
  95. }
  96. int And::params() { return 2; }
  97. ExprItem* And::copy() { return new And; }
  98.  
  99. //___________________________________________________________ Neq
  100.  
  101. Neq::Neq(){}
  102. Neq::~Neq(){}
  103. void Neq::process(ValueStack* theStack)
  104. {
  105.   theStack->push(theStack->pop() != theStack->pop());
  106. }
  107. void Neq::print(ValueStack* theStack)
  108. {
  109.   theStack->push(Value("(" + (rcString)theStack->pop() + " != " +
  110.                      (rcString)theStack->pop() + ")"));
  111. }
  112. int Neq::params() { return 2; }
  113. ExprItem* Neq::copy() { return new Neq; }
  114.  
  115. //___________________________________________________________ Eq
  116.  
  117. Eq::Eq(){}
  118. Eq::~Eq(){}
  119. void Eq::process(ValueStack* theStack)
  120. {
  121.   theStack->push(theStack->pop() == theStack->pop());
  122. }
  123. void Eq::print(ValueStack* theStack)
  124. {
  125.   theStack->push(Value("(" + (rcString)theStack->pop() + " == " +
  126.                      (rcString)theStack->pop() + ")"));
  127. }
  128. int Eq::params() { return 2; }
  129. ExprItem* Eq::copy() { return new Eq; }
  130.  
  131. //___________________________________________________________ Lt
  132.  
  133. Lt::Lt(){}
  134. Lt::~Lt(){}
  135. void Lt::process(ValueStack* theStack)
  136. {
  137.   theStack->push(theStack->pop() < theStack->pop());
  138. }
  139. void Lt::print(ValueStack* theStack)
  140. {
  141.   theStack->push(Value("(" + (rcString)theStack->pop() + " < " +
  142.                      (rcString)theStack->pop() + ")"));
  143. }
  144. int Lt::params() { return 2; }
  145. ExprItem* Lt::copy() { return new Lt; }
  146.  
  147. //___________________________________________________________ Leq
  148.  
  149. Leq::Leq(){}
  150. Leq::~Leq(){}
  151. void Leq::process(ValueStack* theStack)
  152. {
  153.   theStack->push(theStack->pop() <= theStack->pop());
  154. }
  155. void Leq::print(ValueStack* theStack)
  156. {
  157.   theStack->push(Value("(" + (rcString)theStack->pop() + " <= " +
  158.                      (rcString)theStack->pop() + ")"));
  159. }
  160. int Leq::params() { return 2; }
  161. ExprItem* Leq::copy() { return new Leq; }
  162.  
  163. //___________________________________________________________ Gt
  164.  
  165. Gt::Gt(){}
  166. Gt::~Gt(){}
  167. void Gt::process(ValueStack* theStack)
  168. {
  169.   theStack->push(theStack->pop() > theStack->pop());
  170. }
  171. void Gt::print(ValueStack* theStack)
  172. {
  173.   theStack->push(Value("(" + (rcString)theStack->pop() + " > " +
  174.                      (rcString)theStack->pop() + ")"));
  175. }
  176. int Gt::params() { return 2; }
  177. ExprItem* Gt::copy() { return new Gt; }
  178.  
  179. //___________________________________________________________ Geq
  180.  
  181. Geq::Geq(){}
  182. Geq::~Geq(){}
  183. void Geq::process(ValueStack* theStack)
  184. {
  185.   theStack->push(theStack->pop() >= theStack->pop());
  186. }
  187. void Geq::print(ValueStack* theStack)
  188. {
  189.   theStack->push(Value("(" + (rcString)theStack->pop() + " >= " +
  190.                      (rcString)theStack->pop() + ")"));
  191. }
  192. int Geq::params() { return 2; }
  193. ExprItem* Geq::copy() { return new Geq; }
  194.  
  195. //___________________________________________________________ Add
  196.  
  197. Add::Add(){}
  198. Add::~Add(){}
  199. void Add::process(ValueStack* theStack)
  200. {
  201.   theStack->push(theStack->pop() + theStack->pop());
  202. }
  203. void Add::print(ValueStack* theStack)
  204. {
  205.   theStack->push(Value("(" + (rcString)theStack->pop() + " + " +
  206.                      (rcString)theStack->pop() + ")"));
  207. }
  208. int Add::params() { return 2; }
  209. ExprItem* Add::copy() { return new Add; }
  210.  
  211. //___________________________________________________________ Sub
  212.  
  213. Sub::Sub(){}
  214. Sub::~Sub(){}
  215. void Sub::process(ValueStack* theStack)
  216. {
  217.   theStack->push(theStack->pop() - theStack->pop());
  218. }
  219. void Sub::print(ValueStack* theStack)
  220. {
  221.   theStack->push(Value("(" + (rcString)theStack->pop() + " - " +
  222.                      (rcString)theStack->pop() + ")"));
  223. }
  224. int Sub::params() { return 2; }
  225. ExprItem* Sub::copy() { return new Sub; }
  226.  
  227. //___________________________________________________________ Mul
  228.  
  229. Mul::Mul(){}
  230. Mul::~Mul(){}
  231. void Mul::process(ValueStack* theStack)
  232. {
  233.   theStack->push(theStack->pop() * theStack->pop());
  234. }
  235. void Mul::print(ValueStack* theStack)
  236. {
  237.   theStack->push(Value("(" + (rcString)theStack->pop() + " * " +
  238.                      (rcString)theStack->pop() + ")"));
  239. }
  240. int Mul::params() { return 2; }
  241. ExprItem* Mul::copy() { return new Mul; }
  242.  
  243. //___________________________________________________________ Div
  244.  
  245. Div::Div(){}
  246. Div::~Div(){}
  247. void Div::process(ValueStack* theStack)
  248. {
  249.   theStack->push(theStack->pop() / theStack->pop());
  250. }
  251. void Div::print(ValueStack* theStack)
  252. {
  253.   theStack->push(Value("(" + (rcString)theStack->pop() + " / " +
  254.                      (rcString)theStack->pop() + ")"));
  255. }
  256. int Div::params() { return 2; }
  257. ExprItem* Div::copy() { return new Div; }
  258.  
  259. //___________________________________________________________ Mod
  260.  
  261. Mod::Mod(){}
  262. Mod::~Mod(){}
  263. void Mod::process(ValueStack* theStack)
  264. {
  265.   theStack->push(theStack->pop() % theStack->pop());
  266. }
  267. void Mod::print(ValueStack* theStack)
  268. {
  269.   theStack->push(Value("(" + (rcString)theStack->pop() + " % " +
  270.                      (rcString)theStack->pop() + ")"));
  271. }
  272. int Mod::params() { return 2; }
  273. ExprItem* Mod::copy() { return new Mod; }
  274.  
  275. //___________________________________________________________ Sin
  276.  
  277. Sin::Sin(){}
  278. Sin::~Sin(){}
  279. void Sin::process(ValueStack* theStack)
  280.   theStack->push( sin(dtor(real(theStack->pop()))) ); 
  281. }
  282. void Sin::print(ValueStack* theStack)
  283.   theStack->push(Value("sin " + (rcString)theStack->pop()));
  284. }
  285. int Sin::params() { return 1; }
  286. ExprItem* Sin::copy() { return new Sin; }
  287.  
  288. //___________________________________________________________  Cos
  289.  
  290. Cos::Cos(){}
  291. Cos::~Cos(){}
  292. void Cos::process(ValueStack* theStack)
  293.   theStack->push(cos(dtor(real(theStack->pop()))) ); 
  294. }
  295. void Cos::print(ValueStack* theStack)
  296.   theStack->push(Value("cos " + (rcString)theStack->pop()));
  297. }
  298. int Cos::params() { return 1; }
  299. ExprItem* Cos::copy() { return new Cos; }
  300.  
  301. //___________________________________________________________ Tan
  302.  
  303. Tan::Tan(){}
  304. Tan::~Tan(){}
  305. void Tan::process(ValueStack* theStack)
  306.   theStack->push( tan(dtor(real(theStack->pop()))) ); 
  307. }
  308. void Tan::print(ValueStack* theStack)
  309.   theStack->push(Value("tan " + (rcString)theStack->pop()));
  310. }
  311. int Tan::params() { return 1; }
  312. ExprItem* Tan::copy() { return new Tan; }
  313.  
  314. //___________________________________________________________ Asin
  315.  
  316. Asin::Asin(){}
  317. Asin::~Asin(){}
  318. void Asin::process(ValueStack* theStack)
  319.   theStack->push( rtod(asin(real(theStack->pop()))) ); 
  320. }
  321. void Asin::print(ValueStack* theStack)
  322.   theStack->push(Value("asin " + (rcString)theStack->pop()));
  323. }
  324. int Asin::params() { return 1; }
  325. ExprItem* Asin::copy() { return new Asin; }
  326.  
  327. //___________________________________________________________ Acos
  328.  
  329. Acos::Acos(){}
  330. Acos::~Acos(){}
  331. void Acos::process(ValueStack* theStack)
  332.   theStack->push( rtod(acos(real(theStack->pop()))) ); 
  333. }
  334. void Acos::print(ValueStack* theStack)
  335.   theStack->push(Value("acos " + (rcString)theStack->pop()));
  336. }
  337. int Acos::params() { return 1; }
  338. ExprItem* Acos::copy() { return new Acos; }
  339.  
  340. //___________________________________________________________ Atan
  341.  
  342. Atan::Atan(){}
  343. Atan::~Atan(){}
  344. void Atan::process(ValueStack* theStack)
  345.   theStack->push( rtod(atan(real(theStack->pop()))) ); 
  346. }
  347. void Atan::print(ValueStack* theStack)
  348.   theStack->push(Value("atan " + (rcString)theStack->pop()));
  349. }
  350. int Atan::params() { return 1; }
  351. ExprItem* Atan::copy() { return new Atan; }
  352.  
  353. //___________________________________________________________ Abs
  354.  
  355. Abs::Abs(){}
  356. Abs::~Abs(){}
  357. void Abs::process(ValueStack* theStack)
  358.   theStack->push(fabs(real(theStack->pop())) ); 
  359. }
  360. void Abs::print(ValueStack* theStack)
  361.   theStack->push(Value("abs " + (rcString)theStack->pop()));
  362. }
  363. int Abs::params() { return 1; }
  364. ExprItem* Abs::copy() { return new Abs; }
  365.  
  366. //___________________________________________________________ Sqrt
  367.  
  368. Sqrt::Sqrt(){}
  369. Sqrt::~Sqrt(){}
  370. void Sqrt::process(ValueStack* theStack)
  371.   theStack->push(sqrt(real(theStack->pop())) ); 
  372. }
  373. void Sqrt::print(ValueStack* theStack)
  374.   theStack->push(Value("sqrt " + (rcString)theStack->pop()));
  375. }
  376. int Sqrt::params() { return 1; }
  377. ExprItem* Sqrt::copy() { return new Sqrt; }
  378.  
  379. //___________________________________________________________ Pow
  380.  
  381. Pow::Pow(){}
  382. Pow::~Pow(){}
  383. void Pow::process(ValueStack* theStack)
  384.   theStack->push( pow(real(theStack->pop()), real(theStack->pop())) );
  385. }
  386. void Pow::print(ValueStack* theStack)
  387. {
  388.   theStack->push(Value("(" + (rcString)theStack->pop() + " ** " +
  389.                      (rcString)theStack->pop() + ")"));
  390. }
  391. int Pow::params() { return 2; }
  392. ExprItem* Pow::copy() { return new Pow; }
  393.  
  394. //___________________________________________________________ Exp
  395.  
  396. Exp::Exp(){}
  397. Exp::~Exp(){}
  398. void Exp::process(ValueStack* theStack)
  399.   theStack->push( exp(real(theStack->pop())) ); 
  400. }
  401. void Exp::print(ValueStack* theStack)
  402.   theStack->push(Value("exp " + (rcString)theStack->pop()));
  403. }
  404. int Exp::params() { return 1; }
  405. ExprItem* Exp::copy() { return new Exp; }
  406.  
  407. //___________________________________________________________ Log
  408.  
  409. Log::Log(){}
  410. Log::~Log(){}
  411. void Log::process(ValueStack* theStack)
  412.   theStack->push( log(real(theStack->pop())) ); 
  413. }
  414. void Log::print(ValueStack* theStack)
  415.   theStack->push(Value("log " + (rcString)theStack->pop()));
  416. }
  417. int Log::params() { return 1; }
  418. ExprItem* Log::copy() { return new Log; }
  419.  
  420. //___________________________________________________________ Log10
  421.  
  422. Log10::Log10(){}
  423. Log10::~Log10(){}
  424. void Log10::process(ValueStack* theStack)
  425.   theStack->push( log10(real(theStack->pop())) ); 
  426. }
  427. void Log10::print(ValueStack* theStack)
  428.   theStack->push(Value("log10 " + (rcString)theStack->pop()));
  429. }
  430. int Log10::params() { return 1; }
  431. ExprItem* Log10::copy() { return new Log10; }
  432.  
  433. //___________________________________________________________ Rand
  434.  
  435. Rand::Rand(){}
  436. Rand::~Rand(){}
  437. void Rand::process(ValueStack* theStack)
  438.   theStack->push(real(drand48())); 
  439. }
  440. void Rand::print(ValueStack* theStack)
  441.   theStack->push(Value("Rand() "));
  442. }
  443. int Rand::params() { return -1; }
  444. ExprItem* Rand::copy() { return new Rand; }
  445.  
  446. /*___________________________________________________________ Gauss
  447.  *
  448.  * Gaussian random number generator.
  449.  */
  450.  
  451. Gauss::Gauss()
  452. {
  453.   Nrand = 4;
  454.   GaussAdd = sqrt(3*Nrand);
  455.   GaussFac = 2*GaussAdd/Nrand;
  456. }
  457. Gauss::~Gauss(){}
  458. void Gauss::process(ValueStack* theStack)
  459.   real sum = 0;
  460.   for (int i=0; i<Nrand; i++)
  461.     sum += drand48();
  462.   theStack->push(real(GaussFac*sum-GaussAdd)); 
  463. }
  464. void Gauss::print(ValueStack* theStack)
  465.   theStack->push(Value("Gauss() "));
  466. }
  467. int Gauss::params() { return -1; }
  468. ExprItem* Gauss::copy() { return new Gauss; }
  469.  
  470. //___________________________________________________________ If
  471.  
  472. If::If(){}
  473. If::~If(){}
  474. void If::process(ValueStack* theStack)
  475.   if (theStack->pop()) {
  476.     Value result = theStack->pop();
  477.     theStack->pop();
  478.     theStack->push(result);
  479.   }
  480.   else {
  481.     theStack->pop();
  482.     // theStack->push(theStack->pop());
  483.   }
  484. }
  485. void If::print(ValueStack* theStack)
  486.   theStack->push(Value("If(" + (rcString)theStack->pop() + ", " 
  487.                     + (rcString)theStack->pop() + ", " 
  488.                     + (rcString)theStack->pop() + ")")); 
  489. }
  490. int If::params() { return 3; }
  491. ExprItem* If::copy() { return new If; }
  492.